package com.badibadi.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import com.badibadi.infos.Results;
import com.badibadi.infos.TypeId;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.mytools.ViewHolder;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class GroupManagementActivity extends BaseActivity implements
		OnClickListener {

	Intent intent = null;
	private ListView activity_group_management_list;
	private AlbumAdapter_new adapter_new;
	private List<TypeId> ids;
	private Results results2;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(GroupManagementActivity.this);
				Utils.showMessage(GroupManagementActivity.this, getResources().getString(R.string.l_net_error));
				break;
			case 2:
				Utils.ExitPrgress(GroupManagementActivity.this);
				adapter_new = new AlbumAdapter_new(GroupManagementActivity.this);
				activity_group_management_list.setAdapter(adapter_new);
				break;
			case 3:
				Utils.ExitPrgress(GroupManagementActivity.this);
				Utils.showMessage(GroupManagementActivity.this, getResources().getString(R.string.l_xa10));
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.activity_group_management);
		activity_group_management_list = (ListView) findViewById(R.id.activity_group_management_list);
		ids = new ArrayList<TypeId>();
		this.findViewById(R.id.addFenzu).setOnClickListener(this);
		this.findViewById(R.id.activity_group_management_back)
				.setOnClickListener(this);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		friendGroup();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.addFenzu:
			intent = new Intent(GroupManagementActivity.this,
					AddAGroupActivity.class);
			break;
		case R.id.activity_group_management_back:
			finish();
			break;
		}
		if (intent != null) {
			startActivity(intent);
			intent = null;
		}
	}

	private void friendGroup() {
		Utils.showPrgress(GroupManagementActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				ids.clear();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", Utils.getUid(GroupManagementActivity.this));
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "chat/friendGroup");
				if (content != null) {
					results2 = Utils.checkResult_NNN(getApplicationContext(),
							content);
//					   ("好友分组返回参数是:" + content);
					if (results2 != null
							&& !results2.getRetmsg().equals("null")
							&& results2.isRet()) {
						try {
							ids = JSONUtils.getListByJsonString(
									results2.getRetmsg(), TypeId.class);
							handler.sendEmptyMessage(2);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

	private class AlbumAdapter_new extends BaseAdapter {
		private Context context;

		public AlbumAdapter_new(Context context) {
			this.context = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return ids.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return ids.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View convertView, ViewGroup arg2) {
			if (convertView == null) {
				convertView = ViewHolder.buildView(context,
						R.layout.activity_group_management_item, arg2);
			}
			ViewHolder viewHolder = (ViewHolder) convertView.getTag();
			TextView name = viewHolder
					.getViewById(R.id.activity_group_management_item_name);
//			   ("名字是:" + ids.get(arg0).getName());
			name.setText(ids.get(arg0).getType());
			name.setTextColor(R.color.black);
			return convertView;
		}
	}

}
